/**
 * 创建视频聊天
 * MediaSourceTrack API 只有最新版的Chrome 支持
 */
debugger
// 获取用户媒体
function hasUserMedia() {
    navigator.getUserMedia = navigator.getUserMedia || navigator.webkitGetUserMedia || navigator.mozGetUserMedia || navigator.msGetUserMedia ;
    return !!navigator.getUserMedia ;
}
// 获取对等连接
function hasRTCPeerConnection(){
    window.RTCPeerConnection = window.RTCPeerConnection || window.webkitRTCPeerConnection || window.mozRTCPeerConnection ;
    return !!window.RTCPeerConnection ;
}

var yourVideo = document.querySelector("#yours"),
 theirVideo = document.querySelector("#theirs"),yourConnection,theirConnection;



if(hasUserMedia()){
    navigator.getUserMedia = navigator.getUserMedia || navigator.webkitGetUserMedia || navigator.mozGetUserMedia || navigator.msGetUserMedia ;
    // 根据不同的浏览器 设置不同的分辨率和其他参数值
       navigator.getUserMedia(
                             {video:true,audio:false},
                             function(stream){
                                 yourVideo.src = window.URL.createObjectURL(stream); // 函数将流加载到video元素中
                                    debugger
                                 if(hasRTCPeerConnection()){
                                     startPeerConnection(stream);
                                 }else{
                                     alert("sorry , your browser does not support webRTC");
                                 }

                                 yourConnection.addStream(stream);
                                 theirConnection.onaddstream = function (e) {
                                     theirVideo.src = window.createObjectURL(e.stream);
                                 };

                                 // 监听流的创建

                             },function (error) {
                                alert("sorry , your browser does not support webRTC");
                             });


}else{
    console.log(" Raised an error when capturiing:",error);
}



function  startPeerConnection(stream) {
    debugger;
    var configuration = {
        // 放开这段注释代码来增加自定义的iceService
        "iceServers":[{"url":"stun:127.0.0.1:9876"}]
       // "iceServers": [{ "url": "stun:stun.1.google.com:19302" }]
    } ;

    yourConnection = new webkitRTCPeerConnection(configuration);
    theirConnection= new webkitRTCPeerConnection(configuration);

    // 创建ICE 处理
    yourConnection.onicecandidate = function (event) {
        if(event.candidate){
            theirConnection.addIceCandidate(new RTCIceCandidate(event.candidate));
        };
    } ;

    theirConnection.onicecandidate = function (event) {
        if(event.candidate){
            yourConnection.addIceCandidate(new RTCIceCandidate(event.candidate));
        };
    } ;
    // 开始 offer
    yourConnection.createOffer(function (offer) {
        yourConnection.setLocalDescription(offer);
        theirConnection.setRemoteDescription(offer);
        theirConnection.createAnswer(function (offer) {
            theirConnection.setLocalDescription(offer);
            yourConnection.setRemoteDescription(offer);
        });
    });


}